package chapter04.part01;

import java.io.IOException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * 使用 Socket 连接到服务器
 * 
 * 把服务器地址和端口发送给 Socket 连接失败会抛出异常
 * 只适合简单服务器连接
 * 
 * @author insight
 * @since 2021/8/29
 */
public class SocketTest {
    public static void main(String[] args) throws IOException {
        try (Socket socket = new Socket("time-a.nist.gov", 13);
             Scanner in = new Scanner(socket.getInputStream(), StandardCharsets.UTF_8)) {
            while (in.hasNextLine()) {
                String s = in.nextLine();
                System.out.println(s);
            }
        }
    }
}